package com.asa.jihe.utils;

import java.util.List;

public class ToLeftTest {
	
	
	/**
	 * toleft测试，true为在直线的左侧
	 * pqs是逆时针为整数true
	 * pqs是顺时针false
	 * pqs在一条直线上，也是false	area2的计算结果是0
	 * @param p	测试点，这个点是否在qs组成的直线左侧
	 * @param q	初始点
	 * @param s	结束点
	 * @return
	 */
	public static boolean toLeftTest(Point p, Point q, Point s){
	     return area2(p,q,s) > 0;
	}
	public static boolean toOnLineTest(Point p, Point q, Point s){
	     return area2(p,q,s) == 0;
	}
	
	public static boolean toLeftOnLineTest(Point p, Point q, Point s){
	     return area2(p,q,s) >= 0;
	}
	
	
	 
	public static double area2(Point p, Point q, Point s){
	     return p.x * q.y - p.y * q.x + q.x * s.y - q.y * s.x + s.x * p.y - s.y * p.x;
	}
	
	/**
	 * 判断一个点是否在一个由三点构成的三角形内
	 * @param r 这个就是需要判断的 点
	 * @param p
	 * @param q
	 * @param s
	 * @return
	 */
	public static boolean inTriangleTest(Point r, Point p, Point q, Point s){
		boolean a = toLeftTest(p,q,r);
		boolean b = toLeftTest(q,s,r);
		boolean c = toLeftTest(s,p,r);
	    return ( a ==  b && b == c);
	}
	
	
	/**
	 * 闭合的线段，是否包围了一个点
	 * @param spoint
	 * @param lines0
	 * @return
	 */
	public static boolean containtpoint(Point point, List<Line> lines) {
		// TODO Auto-generated method stub
		
		for (int i = 0; i < lines.size(); i++) {
			Line line = lines.get(i);
			
			Point spoint = line.spoint;
			Point epoint = line.epoint;
			
			
			boolean leftTest = toLeftTest(point, spoint, epoint);
			if (!leftTest) {
				return false;
			}
		}
		
		return true;
	}
	
	
	
	
	/**
	 * 同侧检验
	 * @param points
	 * @param s
	 * @param e
	 * @return
	 */
	public static boolean tongce(List<Point> points, Point s, Point e){
		
		
//		for (int i = 0; i < points.size(); i++) {
//			
//			
//			Point point = points.get(0);
//			
//			if (point.equals(s)||point.equals(e)) {
//				continue;
//			}
//			
//			
//			if (toLeftTest(point, s, e)) {
//				
//			}else {
////				left = false;
//				continue;
//			}
//			
//			break;
//			
//		}
		
		
		
		
		for (int i = 0; i < points.size(); i++) {
			Point point = points.get(i);
			
			if (point.equals(s)||point.equals(e)) {
				continue;
			}
			
			
			if (toLeftTest(point, s, e)) {
					continue;
				
			}else if(!toOnLineTest(point, s, e)){
				return false;
			}
			
			
		}
		
		
		
		
		
		
		return true;
		
		
		
	}
	
	
	
	
	
	

}
